package com.brother.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.brother.database.DataBase;
import com.brother.ibatis.ClientFactory;
import com.brother.model.Alarm;
import com.brother.model.AlarmType;
import com.brother.realtime.ProcessorPool;
import com.brother.realtime.RealTimeDataProcessor;
import com.brother.realtime.config.ConfigGEO;
import com.brother.realtime.config.OverSpeedConfig;
import com.ibatis.sqlmap.client.SqlMapClient;

public class AlarmService {

public void addOrUpdateAlarm(Alarm al){
		SqlMapClient map = ClientFactory.getSqlMapper();
		try {
			List<Alarm> als = map.queryForList("selectAlarmByExample", al);
			if(als == null || als.size() == 0){
				log.debug("insert alarm:"+al.getAlarmType()+" "+al.getCondition()
						+" "+al.getData()+" "+al.getName()+" "+al.getState()+" "+al.getTrackerId()
						+" "+al.getValue1()+" "+al.getValue2()+" "+al.getValue3()
						+" "+al.getValue4());
				map.insert("insertAlarm",al);
			}else{
				al.setAlarmId(als.get(0).getAlarmId());
				map.update("updateAlarm", al);
				log.debug("update alarm:"+al.getAlarmType()+" "+al.getCondition()
						+" "+al.getData()+" "+al.getName()+" "+al.getState()+" "+al.getTrackerId()
						+" "+al.getValue1()+" "+al.getValue2()+" "+al.getValue3()
						+" "+al.getValue4());
			}
		} catch (SQLException e) {
			log.error(e);
		}
	}

/**添加或则更新速度报警功能
 * @param trackerid
 * @param value1
 */
public void addSpeedAlarm(String trackerid,float value1, String workable){
	SqlMapClient map = ClientFactory.getSqlMapper();
	Alarm al = new Alarm();
	al.setTrackerId(trackerid);
	al.setAlarmType(AlarmType.Over_Speed_Alarm.toString());
	
	try {
		List<Alarm> als = map.queryForList("selectAlarmByExample", al);
		al.setValue1(value1);
		al.setData(value1 + "");
		al.setSetSuccess(0);
		al.setState(workable);
		if(als.size() == 0){
			al.setCreateTime(new Date());
			al.setCondition("0");
			al.setState("1");
			map.insert("insertAlarm",al);
		}else{ //  更新报警器
			al.setAlarmId(als.get(0).getAlarmId());
			map.update("updateAlarm", al);
		}
		
	} catch (SQLException e) {
		Logger.getLogger(this.getClass()).error(e);
	}
	
	OverSpeedConfig cs = new OverSpeedConfig();
	cs.setSpeed(value1);
	cs.setAction(workable);
	RealTimeDataProcessor p = ProcessorPool.getInstance().getProcessorByTrackerId(al.getTrackerId());
	if(p != null && p.isLogin()){
		p.addConfig(cs);
	}
}

/**添加或则更新Geo_Fence_Alarm报警功能
 * @param trackerid
 * @param value1
 */
public void addGeoFenceAlarm(Alarm al){
	SqlMapClient map = ClientFactory.getSqlMapper();
	try {
		al.setCreateTime(new Date());
		al.setAlarmType(AlarmType.Geo_Fence_Alarm.toString());
		map.insert("insertAlarm", al);
	} catch (SQLException e) {
		Logger.getLogger(this.getClass()).error(e);
	}
	//this.setGEOToGPS(al);
}

public boolean deleteAlarm(String alarmId){
	SqlMapClient map = ClientFactory.getSqlMapper();
	try {
		return map.delete("deleteAlarm", Integer.parseInt(alarmId)) > 0;
	} catch (SQLException e) {
		Logger.getLogger(this.getClass()).error(e);
	}
	return false;
}

private void setGEOToGPS(Alarm al) {
	RealTimeDataProcessor p = ProcessorPool.getInstance().getProcessorByTrackerId(al.getTrackerId());
	if(p != null && p.isLogin()){
		ConfigGEO cg = new ConfigGEO();
		String data = al.getData(); //  114.000,22.00;114.03,22.05
		String[] datas = data.split(";");
		cg.setSwLat(datas[0].split(",")[0]);
		cg.setSwLng(datas[0].split(",")[1]);
		cg.setNeLat(datas[1].split(",")[0]);
		cg.setNeLng(datas[1].split(",")[1]);		
		p.addConfig(cg);
	}
	
}

public void updateGeoFenceAlarm(Alarm al){
	SqlMapClient map = ClientFactory.getSqlMapper();
	try {
		map.insert("updateAlarm", al);
	} catch (SQLException e) {
		Logger.getLogger(this.getClass()).error(e);
	}
	//this.setGEOToGPS(al);
}

public void updateGeoAlarmSucStatus(int status , String trackerId){
	String sql ="update alarm set setSuccess="+status+"  where trackerid='"+trackerId+"' and alarmType='"+AlarmType.Geo_Fence_Alarm+"'";;
	if(log.isDebugEnabled())
		log.debug(sql);
	DataBase.update(sql);
	DataBase.closeConnection();	
}

public void updateSpeedAlarmSucStatus(int status , String trackerId){
	String sql ="update alarm set setSuccess="+status+"  where trackerid='"+trackerId+"' and alarmType='"+AlarmType.Over_Speed_Alarm+"'";
	if(log.isDebugEnabled())
		log.debug(sql);
	DataBase.update(sql);
	DataBase.closeConnection();	
}

private Logger log = Logger.getLogger(this.getClass());

public List<Alarm> findAlarms(Alarm alarm){
	List<Alarm> als = new ArrayList();
	SqlMapClient map = ClientFactory.getSqlMapper();
	try{
		als = map.queryForList("selectAlarmByExample", alarm);
	}catch(SQLException e){
		Logger.getLogger(this.getClass()).error(e);
	}
	return als;
}


}
